package blog.controller;

import blog.common.Result;
import blog.entity.Action;
import blog.entity.Video;
import blog.service.ActionService;
import blog.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/action")
@Slf4j
public class ActionController {
    @Autowired
    private ActionService actionService;
    @Autowired
    private VideoService videoService;

    /*

     */


    /*
    * 添加用户行为
    * http://localhost:8080/action
    * {
    "uid":"1",
    "vid":"1",
    "isView":"1",
    "isCollect":"1",
    "videoTime":600,
    "readTime":60
}
     */
    @PostMapping
    public Result addAction(@RequestBody Action action) {
        //1.判断参数是否合法
        if (action.getUid() == null || action.getVid() == null) {
            return Result.error("参数不合法");
        }
        //2.调用service层的方法
        //2.1判断是否已经存在该用户行为
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid, action.getUid());
        wrapper.eq(Action::getVid, action.getVid());
        Action one = actionService.getOne(wrapper);
        if (one != null) {
            //2.2如果存在，更新用户行为
            one.setIsView(action.getIsView());
            one.setIsCollect(action.getIsCollect());
            one.setVideoTime(action.getVideoTime());
            one.setReadTime(action.getReadTime());
            boolean update = actionService.updateById(one);
            if (update) {


                return Result.success(one, "更新成功");
            } else {
                return Result.error("更新失败");
            }
        }
           //2.3如果不存在，添加用户行为
            boolean save = actionService.save(action);
            if (save) {
                return Result.success(null, "添加成功");
            } else {
                return Result.error("添加失败");
            }
    }
    /*
    根据用户id和视频id查询用户行为
     */
    @GetMapping("/getActionByUidAndVid")
    public Result getActionByUidAndVid(@RequestParam(value="uid") Integer uid,
                                       @RequestParam(value="vid",required = false) Integer vid) {
        //1.判断参数是否合法
        if (uid == null) {
            return Result.error("参数不合法");
        }

        //2.调用service层的方法
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid, uid);
        if (vid != null) {
            wrapper.eq(Action::getVid, vid);
            Action one = actionService.getOne(wrapper);
            if (one != null) {
                return Result.success(one, "查询成功");
            } else {
                return Result.error("查询失败");
            }
        }
        List<Action> list = actionService.list(wrapper);
        if (list != null) {
            return Result.success(list, "查询成功");
        } else {
            return Result.error("查询失败");
        }


    }




    /*
    * 获取用户行为
    * http://localhost:8080/action/1?type=0 &page=1&pageSize=10&videoName=你的名字&beginTime=2002/4/15&endTime=2023/5/15
    * 0视频记录，1收藏列表
     */
    @GetMapping("/{uid}")
    public Result getAction(@PathVariable("uid") Integer uid, @RequestParam("type") Integer type,
                            @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize,
                            @RequestParam(value = "videoName", required = false) String videoName,
                            @RequestParam(value = "beginTime", required = false) String beginTime,
                            @RequestParam(value = "endTime", required = false) String endTime) {
        //1.判断参数是否合法
        if (uid == null || type == null || page == null || pageSize == null) {
            return Result.error("参数不合法");
        }
        //2.调用service层的方法
        Page<Action> actionPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid, uid);
        if (type == 0) {
            wrapper.eq(Action::getIsView, 1);
        } else {
            wrapper.eq(Action::getIsCollect, 1);
        }
        if (videoName != null) {
            if (videoService.getVideoIdList(videoName)==null)
            {
                return Result.error("没有该视频");
            }
            wrapper.in(Action::getVid, videoService.getVideoIdList(videoName));

        }
        if (beginTime != null) {
            wrapper.ge(Action::getCreateTime, beginTime);
        }
        if (endTime != null) {
            wrapper.le(Action::getCreateTime, endTime);
        }
        wrapper.orderByDesc(Action::getCreateTime);
        Page<Action> actionPage1 = actionService.page(actionPage, wrapper);
        //3.返回结果
        return Result.success(actionPage1, "查询成功");
    }


    /*
    * 删除单条播放记录/收藏记录
    * http://localhost:8080/action/1?type=0
    * 0视频记录，1收藏列表
     */
    @DeleteMapping("/{aid}")
    public Result deleteAction(@PathVariable("aid") Integer aid, @RequestParam("type") Integer type) {
        //1.判断参数是否合法
        if (aid == null || type == null) {
            return Result.error("参数不合法");
        }
        //2.调用service层的方法
        Action action = actionService.getById(aid);
        if (action == null) {
            return Result.error("没有该条记录");
        }
        if (type == 0) {
            action.setIsView(0);
        } else {
            action.setIsCollect(0);
        }
        boolean update = actionService.updateById(action);
        if (update) {
            return Result.success(null, "删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
    /*
    * 删除该用户所有播放记录/收藏记录
    * http://localhost:8080/action?uid=1&type=0
    * 0视频记录，1收藏列表
     */
    @DeleteMapping
    public Result deleteAllAction(@RequestParam("uid") Integer uid, @RequestParam("type") Integer type) {
        //1.判断参数是否合法
        if (uid == null || type == null) {
            return Result.error("参数不合法");
        }
        //2.调用service层的方法
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid, uid);
        if (type == 0) {
            wrapper.eq(Action::getIsView, 1);
        } else {
            wrapper.eq(Action::getIsCollect, 1);
        }
        List<Action> list = actionService.list(wrapper);
        if (list == null) {
            return Result.error("没有该条记录");
        }
        for (Action action : list) {
            if (type == 0) {
                action.setIsView(0);
            } else {
                action.setIsCollect(0);
            }
            actionService.updateById(action);
        }
        return Result.success(null, "删除成功");

    }


}